<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Model\Article;
use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Article::paginate(4);
        return view('admin.article.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = (new Category)->tree();
        return view('admin.article.add', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $filename = '';
        if($request->hasFile('art_thumb')){
            $filename = time().'.jpg';
            $request->file('art_thumb')->move('./uploads', $filename);
        }
        $input = $request->except('_token', 'art_thumb');
        $message = [
            'art_title.required'=>'文章名称不能为空',
            'art_content.required'=>'文章内容不能为空'

        ];
        $this->validate($request, [
            'art_title'=>'required',
            'art_content'=>'required'
        ],$message);
        $input['art_thumb'] = $filename;
        $input['art_time'] = time();
        if(Article::create($input)){
            return redirect('admin/article');
        }else{
            return back()->with('errors', '文章添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $field = Article::find($id);
        $data = (new Category)->tree();
        return view('admin.article.edit', compact('field', 'data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $oldfilename = Article::find($id)->art_thumb;
        $filename = '';
        if($request->hasFile('art_thumb')){
            unlink("./uploads/".$oldfilename);
            $filename = time().'.jpg';
            $request->file('art_thumb')->move('./uploads', $filename);
        }
        $input = $request->except('_method', 'art_thumb', '_token');
        $message = [
            'art_title.required'=>'文章名称不能为空',
            'art_content.required'=>'文章内容不能为空',
        ];
        $this->validate($request, [
            'art_title'=>'required',
            'art_content'=>'required',
        ],$message);
        $input['art_thumb'] = $filename;
        $input['art_time'] = time();
        if(Article::create($input)){
            return redirect('admin/article');
        }else{
            return back()->with('errors', '文章修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($art_id)
    {
        //删除缩略图
        $info = Article::find($art_id);
        $thumb = $info->art_thumb;
        if($thumb!=""){
            unlink("./uploads/".$thumb);
        }
        $re = Article::where('art_id', $art_id)->delete();
        if($re){
            $data = [
                'status'=>0,
                'msg'=>'文章删除成功',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'文章删除失败',
            ];
        }
        return $data;
    }
}
